package inventory.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import inventory.common.CommonConstants;
import inventory.common.ResultData;
import inventory.common.RoleLevelEnum;
import inventory.entity.Role;
import inventory.entity.po.NoticePo;
import inventory.entity.po.ReparationPo;
import inventory.entity.po.UserPo;
import inventory.service.FeedbackService;
import inventory.service.RoleService;
import inventory.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author RenHaiquan
 * @create 2019-10-24 23:31
 */
@Api(tags = "公告上传/删除/查看 设备保修增删改查")
@RestController
@RequestMapping(CommonConstants.API_VERSION+"/feedback")
public class FeedbackController {
    @Autowired
    FeedbackService feedbackService;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    /**
     * 前端传入上传者名字launcher，要userId
     * 公告内容noticeContext，
     * 上传公告
     * @return
     */
    @ApiOperation(value = "上传一条公告",notes = "")
    @PostMapping(value = "/addNotice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "launcher", value = "上传者名字，要用userId"),
            @ApiImplicitParam(name = "noticeContext", value = "上传的公告信息")
    })
    public ResultData addNotice(String launcher, String noticeContext){
        feedbackService.addNotice(launcher,noticeContext);

        return new ResultData().OK().setObjs(noticeContext);
    }

    /**
     * 前端传入公告id noticeId
     * 删除公告
     * @return
     */
    @ApiOperation(value = "根据公告id删除公告")
    @PostMapping(value = "/deleteNoticeById")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "noticeId", value = "公告id")
    )
    public ResultData deleteNoticeById(String noticeId){
        feedbackService.deleteNoticeById(noticeId);
        return new ResultData().OK().setObjs("公告删除成功");
    }
    /**
     *
     */
    @ApiOperation(value = "前端传入userId，即可直接查询到本市所有公告")
    @GetMapping(value = "/selectNoticeByUserId")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "userId", value = "用户id")
    )
    public ResultData selectNoticeByUserId(String userId){

        //先通过userId拿到roleId 确定是哪个市roleId,然后得到该市下面的区县，然后把所有的公告查出来，比较roleId,
        UserPo userPo = userService.queryUserPoByUserId(userId);
        List<NoticePo> noticePos1 = new ArrayList<>();

        if(userPo.getState().equals("0")){
            //得到县区级(通过底层员工)
            Role role = roleService.selectOne(new EntityWrapper<Role>().eq("role_id", userPo.getPid()));
            //得到市级
            Role role1 = roleService.selectOne(new EntityWrapper<Role>().eq("role_id", role.getPid()));

            //得到了市级下面的区县
            List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("pid", role1.getRoleId()));

            List<NoticePo> noticePos = feedbackService.selectAllNoticePo();


            for (NoticePo noticePo:noticePos) {
                for (Role role2:roles) {
                    if (noticePo.getRoleId().equals(role2.getRoleId())){
                        noticePos1.add(noticePo);
                    }
                }
                if (noticePo.getRoleId().equals(role1.getRoleId())){
                    noticePos1.add(noticePo);
                }

            }

        }else if (userPo.getState().equals("1")){
            //得到市级
            Role role = roleService.selectOne(new EntityWrapper<Role>().eq("role_id", userPo.getPid()));
            //得到了市级下面的区县
            List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("pid", role.getRoleId()));

            List<NoticePo> noticePos = feedbackService.selectAllNoticePo();

            for (NoticePo noticePo:noticePos) {
                for (Role role2:roles) {
                    if (noticePo.getRoleId().equals(role2.getRoleId())){
                          noticePos1.add(noticePo);}
                }
                if (noticePo.getRoleId().equals(role.getRoleId())){
                    noticePos1.add(noticePo);
                }

            }

        }else if (userPo.getState().equals("2")){
            //得到了市级下面的区县
            List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("pid", userPo.getRoleId()));
            List<NoticePo> noticePos = feedbackService.selectAllNoticePo();

            for (NoticePo noticePo:noticePos) {
                for (Role role2:roles) {
                    if (noticePo.getRoleId().equals(role2.getRoleId())){
                        noticePos1.add(noticePo);
                    }
                }
                if (noticePo.getRoleId().equals(userPo.getRoleId())){
                    noticePos1.add(noticePo);

                }

            }
        }

        return new ResultData().OK().setObjs(noticePos1);
    }


    /**
     *前端传入上传者名字launcher，要roleId
     */
    @ApiOperation(value = "根据上传者roleId查看同级别上传过的公告")
    @GetMapping(value = "/selectNoticeByLauncherRoleId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "launcherRoleId", value = "要用roleId")
    })
    public ResultData selectNoticeByLauncherRoleId(String launcherRoleId){
        List<NoticePo> notices = feedbackService.selectNoticeByLauncherRoleId(launcherRoleId);
        return new ResultData().OK().setObjs(notices);
    }

    /**根据公告id修改公告
     * 前端传入noticeId
     * @return
     */
    @ApiOperation(value = "根据公告id修改公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "公告id"),
            @ApiImplicitParam(name = "noticeContext", value = "公告内容")
    })
    @PostMapping(value = "/updateNoticeById")
    public ResultData updateNoticeById(String noticeId,String noticeContext){
       feedbackService.updateNoticeById(noticeId,noticeContext);
        return new ResultData().OK().setObjs("修改成功");
    }





    /**
     * 1
     * 前端传入
     * 报修设备串号deviceImei
     * 报修信息描述reparationTaking
     * 报修者reparationerId 要userId
     * 报修指向reparationPointer 要roleId
     * 报修处理进度handleProgress  1
     * 新增保修
     * @return
     */
    @ApiOperation(value = "新增一条设备保修数据",
            notes = "handleProgress：这里作出解释。" +
                    " 1代表个人向县或者区上报" +
                    "2代表县或者区不通过 3代表县或者区通过 " +
                    "4代表市不通过 5代表市通过")
    @RequestMapping(value = "/addRepair",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceImei", value = "报修信息设备串号"),
            @ApiImplicitParam(name = "reparationTaking", value = "报修信息描述"),
            @ApiImplicitParam(name = "reparationerId", value = "报修者,要userId"),
            @ApiImplicitParam(name = "handleProgress", value = "个人：1  县区：2   市级：4")
    })
    public ResultData addRepair(String deviceImei,
                                String reparationTaking,
                                String reparationerId,
                                String handleProgress) {
        ReparationPo reparationPo = new ReparationPo();

        reparationPo.setReparationId(UUID.randomUUID().toString().replace("-",""));
        reparationPo.setDeviceImei(deviceImei);
        reparationPo.setReparationTaking(reparationTaking);
        reparationPo.setReparationerId(reparationerId);
        UserPo userPo = userService.queryUserPoByUserId(reparationerId);//查询当前登录用户
        switch (Objects.requireNonNull(RoleLevelEnum.getRoleLevelByCode(userPo.getState()))){
            case PERSON:
                Role role = feedbackService.selectPidByReparationerId(reparationerId);//即userId
                reparationPo.setReparationPointerCounty(role.getPid());
                Role role1 = feedbackService.selectPidByRoleId(role.getPid());
                reparationPo.setReparationPointerCity(role1.getPid());
                reparationPo.setHandleProgress(handleProgress);
                break;
            case COUNTY:
                reparationPo.setReparationPointerCounty(userPo.getRoleId());
//                Role role2 = feedbackService.selectPidByRoleId(userPo.getRoleId());
                reparationPo.setReparationPointerCity(userPo.getPid());
                reparationPo.setHandleProgress("2");
                break;
            case CITY:
                reparationPo.setReparationPointerCounty(null);
                reparationPo.setReparationPointerCity(userPo.getRoleId());
                reparationPo.setHandleProgress("4");
                break;
        }


        feedbackService.insert(reparationPo);

        return new ResultData().OK().setObjs(reparationPo);
    }

    /**
     * 前端传入
     * 保修id reparationId
     * 删除报修成功
     * @return
     */
    @ApiOperation(value = "删除一条设备报修数据")
    @PostMapping(value = "/deleteRepairByReparationId")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "reparationId",value = "设备信息id")
    )
    public ResultData deleteRepairByReparationId(String reparationId){
        int i = feedbackService.deleteReparationPoByreparationId(reparationId);
        return new ResultData().OK().setObjs("删除成功");
    }

    /**1
     * 根据用户id查看报修记录
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id查看报修记录",notes = "需用userId")
    @GetMapping(value = "/queryRepairByUserId")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "userId",value = "需用userId")
    )
    public ResultData queryRepairByUserId(String userId){
        List<ReparationPo> rs = feedbackService.queryRepairByUserId(userId);
        return new ResultData().OK().setObjs(rs);
    }



    /**1
     * 前端传入
     * 报修者reparationerId 要userId
     * 查看用户的所有保修
//     * @return
//     */
//    @ApiOperation(value = "查看用户的所有保修",notes = "即userId")
//    @RequestMapping(value = "/queryRepairByUserId",method = RequestMethod.GET)
//    @ApiImplicitParams(
//            @ApiImplicitParam(name = "reparationerId",value = "reparationerId，即userId")
//    )
//    public ResultData queryRepairByUserId(String reparationerId){
//        List<ReparationPo> reparationPos = feedbackService.selectReparationer(reparationerId);
//        return new ResultData().OK().setObjs(reparationPos);
//
//    }

//    /**1
//     * 根据设备id查看设备维修信息
//     * @param reparationId
//     * @return
//     */
//    @ApiOperation(value = "查看设备维修信息",notes = "必须传入设备id")
//    @RequestMapping(value = "/queryRepairByReparationId",method = RequestMethod.GET)
//    @ApiImplicitParams(
//            @ApiImplicitParam(name = "reparationId",value = "设备id")
//    )
//    public ResultData queryRepairByReparationId(String reparationId){
//       ReparationPo reparationPos = feedbackService.selectReparationById(reparationId);
//        System.out.println(reparationPos);
//        return new ResultData().OK().setObjs(reparationPos);
//
//    }

//    /**1
//     * 查看下级的报修记录
//     * @param reparationerId
//     * @return
//     */
//    @ApiOperation(value = "查看下级用户的报修记录",notes = "必须传入报修指向id，需用userId")
//    @RequestMapping(value = "/queryRepairByReparationer",method = RequestMethod.GET)
//    @ApiImplicitParams(
//            @ApiImplicitParam(name = "reparationerId",value = "报修指向，需用userId")
//    )
//    public ResultData queryRepairReparationPointer(String reparationerId){
//        List<ReparationPo> rs = feedbackService.selectRecordByReparationerId(reparationerId);
//        return new ResultData().OK().setObjs(rs);
//    }


    /**1
     * 前端传入
     * 报修设备id reparationId
     * 处理进度 handleProgress
     * 处理报修设备
     * @return
     */
    @ApiOperation(value = "处理报修设备")
    @PostMapping(value = "/handleRepairById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reparationId", value = "报修设备id"),
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "decide", value = "同意报修1，不同意报修0")
    })
    public ResultData handleRepairById(String reparationId,String userId,String decide){
        int i = feedbackService.updateReparationPoByreparationId(reparationId,userId,decide);
        return new ResultData().OK().setObjs("处理成功");
    }
}
